package org.pro.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

public class Utils {
    public Utils() {
    }

    public static BufferedImage rotateImage(BufferedImage bufferedimage, int degree) {
        int w = bufferedimage.getWidth();
        int h = bufferedimage.getHeight();
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.rotate(Math.toRadians((double)degree), (double)(w / 2), (double)(h / 2));
        graphics2d.drawImage(bufferedimage, 0, 0, (ImageObserver)null);
        graphics2d.dispose();
        return img;
    }

    public static BufferedImage resizeImage(BufferedImage bufferedimage, int w, int h) {
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.drawImage(bufferedimage, 0, 0, w, h, 0, 0, bufferedimage.getWidth(), bufferedimage.getHeight(), (ImageObserver)null);
        graphics2d.dispose();
        return img;
    }

    public static BufferedImage flipImage(BufferedImage bufferedimage) {
        int w = bufferedimage.getWidth();
        int h = bufferedimage.getHeight();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = new BufferedImage(w, h, bufferedimage.getColorModel().getTransparency())).createGraphics()).drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, (ImageObserver)null);
        graphics2d.dispose();
        return img;
    }

    public static double ppDir(double x1, double y1, double x2, double y2) {
        double len_x = x2 - x1;
        double len_y = y2 - y1;
        double radian = Math.atan2(len_y, len_x);//计算弧度的
        return radian * 180.0 / Math.PI;
    }
}

